﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Zvezda.FSO2.DB;

namespace FSO
{
    public partial class UserControlSearchNSIProduct : UserControl, InterfaceSearchControl
    {
        private bool _openOnDblClick;

        public UserControlSearchNSIProduct()
        {
            InitializeComponent();

            radGridViewList.MultiSelect = true;
        }

        #region Interface

        public bool OpenOnDoubleClick
        {
            get
            {
                return _openOnDblClick;
            }
            set
            {
                _openOnDblClick = value;
            }
        }

        public bool EnableFiltering
        {
            get
            {
                return radGridViewList.EnableFiltering;
            }
            set
            {
                radGridViewList.EnableFiltering = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                DataRow dr;
                if (radGridViewList.SelectedRows.Count > 0)
                {
                    dr = (((DataRowView)(radGridViewList.SelectedRows.First()).DataBoundItem)).Row;
                }
                else
                {
                    dr = null;
                }
                return dr;
            }
        }

        public DataSet OwnerDataSet
        {
            get { return dataSetFSO1; }
            set { dataSetFSO1 = (DataSetFSO)value; }
        }

        public GridViewSelectedRowsCollection SelectedRows
        {
            get { return radGridViewList.SelectedRows; }
        }

        public void RefreshList()
        {
            nsI_PRODUCTTableAdapter1.Fill(dataSetFSO1.NSI_PRODUCT);
        }

        #endregion

        private void InitializeGridView()
        {
            GridViewTextBoxColumn colSName = new GridViewTextBoxColumn("SHORT_NAME", dataSetFSO1.NSI_PRODUCT.SHORT_NAMEColumn.ColumnName);
            colSName.HeaderText = "Короткое наименование";

            GridViewTextBoxColumn colFName = new GridViewTextBoxColumn("FULL_NAME", dataSetFSO1.NSI_PRODUCT.FULL_NAMEColumn.ColumnName);
            colFName.HeaderText = "Полное наименование";

            GridViewTextBoxColumn colNDR = new GridViewTextBoxColumn("N_DRAWING", dataSetFSO1.NSI_PRODUCT.N_DRAWINGColumn.ColumnName);
            colNDR.HeaderText = "№ чертежа";

            radGridViewList.MasterTemplate.Columns.Clear();
            radGridViewList.MasterTemplate.Columns.Add(colSName);
            radGridViewList.MasterTemplate.Columns.Add(colFName);
            radGridViewList.MasterTemplate.Columns.Add(colNDR);

            radGridViewList.DataSource = dataSetFSO1.NSI_PRODUCT;
        }

        private void UserControlSearchNSIProduct_Load(object sender, EventArgs e)
        {
            nsI_PRODUCTTableAdapter1.Fill(dataSetFSO1.NSI_PRODUCT);

            this.InitializeGridView();
        }
    }
}
